package com.bkgy.controller.planking.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.controller.planking.dao.profitMapper;
import com.bkgy.controller.planking.entity.ProfitEntity;
import com.bkgy.controller.planking.service.ProfitService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class ProfitServiceImpl extends ServiceImpl<profitMapper, ProfitEntity> implements ProfitService {

    @Autowired
    private profitMapper profitMapper;

    @Override
    public List<ProfitEntity> getProfit(JSONObject json) {
        try {
            String startTime = json.getString("startTime").replaceAll("[^0-9]", "");
            String endTime = json.getString("endTime").replaceAll("[^0-9]", "");
            LambdaQueryWrapper<ProfitEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.isNotNull(ProfitEntity::getTurnL2);
            // 添加creDate时间范围条件
            if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
                wrapper.between(ProfitEntity::getCreDate, startTime, endTime);
            } else if (StringUtils.isNotBlank(startTime)) {
                wrapper.ge(ProfitEntity::getCreDate, startTime);
            } else if (StringUtils.isNotBlank(endTime)) {
                wrapper.le(ProfitEntity::getCreDate, endTime);
            }
            return profitMapper.selectList(wrapper);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
